package com.china08.yxyapi.api.manage.stu.mystudy.huixue.scoreimprove;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 首页--成绩提升(学习计划列表)
 * 
 */
@CrossOrigin
@RestController
public class YxyHuixuePlanSrhController extends YxyApiBaseController {

    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;

    @ApiOperation(value = "成绩提升首页 学习计划列表")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/huixue/scoreimprove/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) RepModel reqModel) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str = reqModel.queryContent;// 搜索框中的章节内容
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        Page<YxyHuixuePlan> page;
        if (str != null && !"".equals(str)) {
            page = yxyHuixuePlanRepository.findByStudentIdAndCurrentScoreAndBookContentNameLike(reqModel.studentId, -1d,
                    str, pageable);
        } else {
            page = yxyHuixuePlanRepository.findByStudentIdAndCurrentScore(reqModel.studentId, -1d, pageable);
        }
        List<RespModel> list = new ArrayList<YxyHuixuePlanSrhController.RespModel>();
        for (YxyHuixuePlan yxyHuixuePlan : page) {
            RespModel model = new RespModel();
            model.id = yxyHuixuePlan.getId();
            model.studentId = yxyHuixuePlan.getStudentId();
            model.subjectName = yxyHuixuePlan.getSubjectName();
            model.bookContentName = yxyHuixuePlan.getBookContentName();
            // 开始时间
            Integer beginDate = yxyHuixuePlan.getBeginDate();
            String beginDateString = null;
            if (beginDate != null && beginDate != 0) {
                beginDateString = String.valueOf(beginDate);
                beginDateString = beginDateString.substring(0, 4) + "-" + beginDateString.substring(4, 6) + "-"
                        + beginDateString.substring(6);
            }
            // 结束时间
            Integer endDate = yxyHuixuePlan.getEndDate();
            String endDateString = null;
            if (endDate != null && endDate != 0) {
                endDateString = String.valueOf(endDate);
                endDateString = endDateString.substring(0, 4) + "-" + endDateString.substring(4, 6) + "-"
                        + endDateString.substring(6);
            }
            Date endDate2 = sdf.parse(endDateString);
            // 如果计划是已截止状态剩余时间是0
            model.resttime = yxyHuixuePlan.getStatus() == 1
                    ? (getIntervalDays(sdf.parse(sdf.format(new Date())), endDate2) + 1) : 0;
            model.beginDate = beginDateString;
            model.endDate = endDateString;
            model.status = yxyHuixuePlan.getStatus();
            model.progress = yxyHuixuePlan.getProgress();
            model.comment = yxyHuixuePlan.getNotice();
            model.iconStatus = yxyHuixuePlan.getIconStatus();
            list.add(model);
        }
        Page<RespModel> respModel = new PageImpl<YxyHuixuePlanSrhController.RespModel>(list, pageable,
                page.getTotalElements());
        return respModel;

    }

    protected static class RepModel extends YxyApiPageReqModel {

        public String queryContent;// 搜索内容(章节名称)

        public String studentId;// 学生id

    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;// 主键(计划id)

        public String studentId;// 学生id

        public String subjectName;// (6学科名称)

        public String bookContentName;// 目录名称(7章节信息。显示创建学习计划时的下次考试范围。)

        public String beginDate;// 开始时间(8)

        public String endDate;// 结束时间

        public Integer resttime;// 剩余时间(9)

        public Integer status;// 0--已截止 , 1--进行中(11学习计划的状态)

        public Double progress;// 掌握度(10即完成度)

        public String comment;// 当前计划点评(12)

        public Integer iconStatus;// 图标状态 1.悲伤 2.微笑 3.惊喜

    }

    private static Integer getIntervalDays(Date fDate, Date oDate) {

        if (null == fDate || null == oDate) {

        return -1;

        }

        long intervalMilli = oDate.getTime() - fDate.getTime();

        return (int) (intervalMilli / (24 * 60 * 60 * 1000));

    }

}
